package aries.database;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import aries.Setting;
import aries.utils.ExceptionManager;
import aries.utils.TextUtil;
import aries.utils.Util;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Query extends Database {

    /**
     * Used when your value is INT.
     */
    public static final String INT = "INT";

    /**
     * Used when your value is VARCHAR.
     */
    public static final String VARCHAR = "VARCHAR";
    
    /**
     * Used when your value is VARCHAR2.
     */
    public static final String VARCHAR2 = "VARCHAR2";
    
    /**
     * Used when your value is TEXT.
     */
    public static final String TEXT = "TEXT";
    
    /**
     * Used when your value is DATE.
     */
    public static final String DATE = "DATE";
    
    /**
     * Used when your value is TINY INT.
     */
    public static final String TINYINT = "TINYINT";
    
    /**
     * Used when your value is SMALLINT.
     */
    public static final String SMALLINT = "SMALLINT";
    
    /**
     * Used when your value is MEDIUMINT.
     */
    public static final String MEDIUMINT = "MEDIUMINT";
    
    /**
     * Used when your value is BIGINT.
     */
    public static final String BIGINT = "BIGINT";
    
    /**
     * Used when your value is DECIMAL.
     */
    public static final String DECIMAL = "DECIMAL";
    
    /**
     * Used when your value is FLOAT.
     */
    public static final String FLOAT = "FLOAT";
    
    /**
     * Used when your value is DOUBLE.
     */
    public static final String DOUBLE = "DOUBLE";
    
    /**
     * Used when your value is REAL.
     */
    public static final String REAL = "REAL";
    
    /**
     * Used when your value is BIT.
     */
    public static final String BIT = "BIT";
    
    /**
     * Used when your value is BOOL.
     */
    public static final String BOOL = "BOOL";
    
    /**
     * Used when your value is SERIAL.
     */
    public static final String SERIAL = "SERIAL";
    
    /**
     * Used when your value is DATETIME.
     */
    public static final String DATETIME = "DATETIME";
    
    /**
     * Used when your value is TIMESTAMP.
     */
    public static final String TIMESTAMP = "TIMESTAMP";
    
    /**
     * Used when your value is TIME.
     */
    public static final String TIME = "TIME";
    
    /**
     * Used when your value is YEAR.
     */
    public static final String YEAR = "YEAR";
    
    /**
     * Used when your value is CHAR.
     */
    public static final String CHAR = "CHAR";
    
    /**
     * Used when your value is TINYTEXT.
     */
    public static final String TINYTEXT = "TINYTEXT";
    
    /**
     * Used when your value is MEDIUMTEXT.
     */
    public static final String MEDIUMTEXT = "MEDIUMTEXT";
    
    /**
     * Used when your value is LONGTEXT.
     */
    public static final String LONGTEXT = "LONGTEXT";
    
    /**
     * Used when your value is BINARY.
     */
    public static final String BINARY = "BINARY";
    
    /**
     * Used when your value is VARBINARY.
     */
    public static final String VARBINARY = "VARBINARY";
    
    /**
     * Used when your value is TINYBLOB.
     */
    public static final String TINYBLOB = "TINYBLOB";
    
    /**
     * Used when your value is MEDIUMBLOB.
     */
    public static final String MEDIUMBLOB = "MEDIUMBLOB";
    
    /**
     * Used when your value is BLOB.
     */
    public static final String BLOB = "BLOB";
    
    /**
     * Used when your value is LONGBLOG.
     */
    public static final String LONGBLOG = "LONGBLOG";
    
    /**
     * Used when your value is ENUM.
     */
    public static final String ENUM = "ENUM";
    
    /**
     * Used when your value is GEOMETRY.
     */
    public static final String GEOMETRY = "GEOMETRY";
    
    /**
     * Used when your value is POINT.
     */
    public static final String POINT = "POINT";
    
    /**
     * Used when your value is LINESTRING.
     */
    public static final String LINESTRING = "LINESTRING";
    
    /**
     * Used when your value is POLYGON.
     */
    public static final String POLYGON = "POLYGON";
    
    /**
     * Used when your value is MULTIPOINT.
     */
    public static final String MULTIPOINT = "MULTIPOINT";
    
    /**
     * Used when your value is MULTILINESTRING.
     */
    public static final String MULTILINESTRING = "MULTILINESTRING";
    
    /**
     * Used when your value is MULTIPOLYGON.
     */
    public static final String MULTIPOLYGON = "MULTIPOLYGON";
    
    /**
     * Used when your value is GEOMETRYCOLLECTION.
     */
    public static final String GEOMETRYCOLLECTION = "GEOMETRYCOLLECTION";
    
    /**
     * Used when your value is SET.
     */
    public static final String SET = "SET";

    /**
     * Ascending sorting.
     */
    public static final String ASCENDING = "ASC";

    /**
     * Descending sorting.
     */
    public static final String DESCENDING = "DESC";
    
    //@TODO : Handle BINARY, GEOMETRY, ENUM, LINESTRING, POINT, POLYGON, SERIAL, SET
    
    public static Boolean isUseFlank(String type) throws IllegalArgumentException {
        if (type == null) {
            throw new IllegalArgumentException("Type cannot be null.");
        }
        Boolean flank = true;
        if (type.equals(Query.BIGINT) || type.equals(Query.BOOL) || type.equals(Query.DECIMAL) || 
            type.equals(Query.DOUBLE) || type.equals(Query.FLOAT) || type.equals(Query.INT) || 
            type.equals(Query.MEDIUMINT) || type.equals(Query.REAL) || type.equals(Query.SMALLINT)) {
            flank = false;
        }
        return flank;
    }

    /**
     * Check if the record is exist.
     *
     * @param table
     * @param field
     * @param value
     * @return isExist
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static Boolean isExist(Table table, String field, String type, String value) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            open();
        Boolean rtn = false;
        if (isUseFlank(type)) {
            value = TextUtil.flank(value, "'");
        }
        if (count(table, new Where(field+" = "+value)) > 0)
            rtn = true;
        if (getAutomatic())
            close();
        return rtn;
    }

    /**
     * Create an auto increment from the field.
     *
     * @param table
     * @return autoIncrement
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static String getAutoIncrement(Table table) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            reset();
        ResultSet rs = select(table, null, table.getPrimaryKey(), DESCENDING);
        rs.next();
        return String.valueOf(rs.getInt(table.getPrimaryKey())+1);
    }
    
    /**
     * Create database from database configuration.
     * 
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static void createDatabase() throws SQLException, ClassNotFoundException {
        createDatabase(getDbName());
    }
    
    /**
     * Create a new database.
     * 
     * @param database
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static void createDatabase(String database) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            open();
        String query = "CREATE DATABASE "+database;
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        executeUpdate(query);
        if (getAutomatic())
            close();
    }
    
    /**
     * Create table from Database Mapping.
     * 
     * @param table
     * @param field
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static void createTable(Table table, Field field) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            open();
        String query = "CREATE TABLE "+table.getTable()+" (";
        for (byte i=0; i<field.getField().length; i++) {
            query += field.getField()[i]+" "+field.getType()[i]+"("+field.getLength()[i]+") ";
            if (table.isAutoIncrement() && i == 0)
                query += "AUTO_INCREMENT ";
            if (i == 0)
                query += "PRIMARY KEY ";
            if (i != field.getLength().length-1) {
                query +=",";
            }
        }
        query += ");";
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        executeUpdate(query);
        if (getAutomatic())
            close();
    }
    
    /**
     * Create table from Database Mapping.
     * 
     * @param table
     * @param field
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static void createTable(Table[] table, Field[] field) throws SQLException, ClassNotFoundException {
        for (byte i=0; i<table.length; i++) {
            Query.createTable(new Table(table[i].getTable()), table[i].getField());
        }
    }

    /**
     * Search from a database with a key.
     * 
     * @param table
     * @param key
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static ResultSet search(Table table, String key) throws SQLException, ClassNotFoundException {
        return search(table, table.getField().getField(), key);
    }
    
    /**
     * Search from a database with a key.
     * 
     * @param table
     * @param theField
     * @param key
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static ResultSet search(Table table, String[] field, String key) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            reset();
        String listField = "";
        for (byte i=0; i<field.length; i++) {
            listField = listField+field[i]+" LIKE '%"+key+"%' ";
            if (i != field.length-1) {
                listField = listField+" OR ";
            }
        }
        String query = "SELECT * FROM "+Database.getDbName()+"."+table.getTable()+" "+new Where(listField).getWhere();
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        return executeQuery(query);
    }
    
    /**
     * Execute a select query.
     *
     * @param table
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static ResultSet select(Table table) throws SQLException, ClassNotFoundException {
        return select(table, null, new Where(""));
    }
    
    /**
     * Execute a select query.
     * 
     * @param table
     * @param projection
     * @param where
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static ResultSet select(Table table, String[] projection, Where where) throws SQLException, ClassNotFoundException {
        String _where = "";
        if (getAutomatic())
            reset();
        if (where != null && !where.getPureWhere().equals(""))
            _where = where.getWhere();
        String theProjection = "*";
        if (projection != null)
            theProjection = TextUtil.join(projection, ",");
        String query = "SELECT "+theProjection+" FROM "+Database.getDbName()+"."+table.getTable()+" "+_where;
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        return executeQuery(query);
    }

    /**
     * Execute a select query.
     *
     * @param table
     * @param field
     * @param sortingType
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static ResultSet select(Table table, String[] projection, String field, String sortingType) throws SQLException, ClassNotFoundException {
        return select(table, projection, new Where(""), field, sortingType);
    }
    
    /**
     * Execute a select query.
     * 
     * @param table
     * @param projection
     * @param where
     * @param field
     * @param sortingType
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static ResultSet select(Table table, String[] projection, Where where, String field, String sortingType) throws SQLException, ClassNotFoundException {
        String _where = "";
        if (getAutomatic()) {
            close();
            open();
        }
        if (where != null && !where.getPureWhere().equals(""))
            _where = where.getWhere();
        String theProjection = "*";
        if (projection != null)
            theProjection = TextUtil.join(projection, ",");
        String query = "SELECT "+theProjection+" FROM "+Database.getDbName()+"."+table.getTable()+" "+_where+" ORDER BY "+field+" "+sortingType;
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        return executeQuery(query);
    }
    
    /**
     * Execute a select query with in where statement.
     * 
     * @param table
     * @param projection
     * @param compared
     * @param in
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static ResultSet selectIn(Table table, String[] projection, String compared, ArrayList in) throws SQLException, ClassNotFoundException {
        return selectIn(table, projection, compared, Util.ObjectToArray(in.toArray()));
    }
    
    /**
     * Execute a select query with in where statement.
     * 
     * @param table
     * @param projection
     * @param compared
     * @param in
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static ResultSet selectIn(Table table, String[] projection, String compared, String[] in) throws SQLException, ClassNotFoundException {
        String _where;
        if (in.length > 0)
            _where = "WHERE "+compared+" IN ("+TextUtil.replacePattern(in, ",", "")+")";
        else
            _where = "";
        if (getAutomatic())
            reset();
        String theProjection = "*";
        if (projection != null)
            theProjection = TextUtil.join(projection, ",");
        String query = "SELECT "+theProjection+" FROM "+Database.getDbName()+"."+table.getTable()+" "+_where;
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        return executeQuery(query);
    }
    
    /**
     * Execute a select query with not in where statement.
     * 
     * @param table
     * @param projection
     * @param compared
     * @param notin
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static ResultSet selectNotIn(Table table, String[] projection, String compared, ArrayList notin) throws SQLException, ClassNotFoundException {
        return selectNotIn(table, projection, compared, Util.ObjectToArray(notin.toArray()));
    }
    
    /**
     * Execute a select query with not in where statement.
     * 
     * @param table
     * @param projection
     * @param compared
     * @param notin
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static ResultSet selectNotIn(Table table, String[] projection, String compared, String[] notin) throws SQLException, ClassNotFoundException {
        String _where;
        if (notin.length > 0)
            _where = "WHERE "+compared+" NOT IN ("+TextUtil.replacePattern(notin, ",", "")+")";
        else
            _where = "";
        if (getAutomatic())
            reset();
        String theProjection = "*";
        if (projection != null)
            theProjection = TextUtil.join(projection, ",");
        String query = "SELECT "+theProjection+" FROM "+Database.getDbName()+"."+table.getTable()+" "+_where;
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        return executeQuery(query);
    }

    /**
     * Count the total record on the table.
     *
     * @param table
     * @return total record
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static int count(Table table) throws SQLException, ClassNotFoundException {
       return Integer.parseInt((String) count(table, null, new Where(""))[0]);
    }

    /**
     * Count the total record on the table.
     *
     * @param table
     * @param where
     * @return int
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static int count(Table table, Where where) throws SQLException, ClassNotFoundException {
        return Integer.parseInt((String) (count(table, null, where)[0]));
    }
    
    /**
     * Count the total record on the table.
     * 
     * @param table
     * @param projection
     * @param where
     * @return int
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static Object[] count(Table table, String[] projection, Where where) throws SQLException, ClassNotFoundException {
        String _where = "";
        if (getAutomatic())
            open();
        if (where != null && !where.getPureWhere().equals("")) {
            _where = where.getWhere();
        }
        String theProjection = "COUNT(*)";
        if (projection != null) {
            theProjection = "COUNT";
            for (byte i=0; i<projection.length; i++) {
                theProjection += "("+projection[i]+"), ";
            }
        }
        String query = "SELECT "+theProjection+" FROM "+Database.getDbName()+"."+table.getTable()+" "+_where;
        ResultSet rs = executeQuery(query);
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        int i=1;
        ArrayList<String> result = new ArrayList<String>();
        while (rs.next()) {
            result.add(rs.getString(i));
            i++;
        }
        if (getAutomatic())
            close();
        return result.toArray();
    }

    /**
     * Insert a data into database.
     *
     * @param table
     * @param value
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static void insert(Table table, String[] value) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            open();
        String[] field = table.getFieldString();
        String[] type = table.getTypeString();
        if (field.length != type.length) {
            throw new SQLException("Aries Framework: field length != type length.");
        }
        if (isUseFlank(type[0].toUpperCase())) {
            value[0] = TextUtil.flank(value[0], "'");
        }
        String theField = field[0], theValue = value[0];
        for (byte i=1; i<field.length; i++) {
            if (isUseFlank(type[i].toUpperCase())) {
                value[i] = "'"+value[i]+"'";
            } else if ((value[i] == null || value[i].equals("")) && (type[i].equals(BIGINT) || type[i].equals(MEDIUMINT) || 
                       type[i].equals(INT) || type[i].equals(SMALLINT))) {
                value[i] = String.valueOf(0);
            }
            theField = theField+","+field[i];
            theValue = theValue+","+value[i];
        }
        String query = "INSERT INTO "+Database.getDbName()+"."+table.getTable()+" ("+theField+") VALUES ("+theValue+")";
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        executeUpdate(query);
        if (getAutomatic())
            close();
    }

    /**
     * Update existing data on database.
     *
     * @param table
     * @param fields
     * @param value
     * @param where
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static void update(Table table, Field fields, String[] value, Where where) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            open();
        String[] field = fields.getField();
        String[] type = fields.getType();
        if (field.length != type.length) {
            throw new SQLException("Aries Framework: field length != type length.");
        }
        field = Util.removeFirstIndex(field);
        type = Util.removeFirstIndex(type);
        if (isUseFlank(type[0].toUpperCase())) {
            value[0] = TextUtil.flank(value[0], "'");
        }
        String set = field[0]+"="+value[0];
        for (byte i=1; i<field.length; i++) {
            if (isUseFlank(type[i].toUpperCase())) {
                value[i] = TextUtil.flank(value[i], "'");
            } else if ((value[i] == null || value[i].equals("")) && (type[i].equals(BIGINT) || type[i].equals(MEDIUMINT) || 
                       type[i].equals(INT) || type[i].equals(SMALLINT))) {
                value[i] = String.valueOf(0);
            }
            set = set+","+field[i]+"="+value[i];
        }
        String query = "UPDATE "+Database.getDbName()+"."+table.getTable()+" SET "+set+" "+where.getWhere();
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        executeUpdate(query);
        if (getAutomatic())
            close();
    }

    /**
     * Delete data from database.
     *
     * @param table
     * @param id
     * @param value
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static void delete(Table table, String id, String value) throws SQLException, ClassNotFoundException {
        if (isUseFlank(table.getPrimaryKeyType().toUpperCase())) {
            value = TextUtil.flank(value, "'");
        }
        delete(table, new Where(id+"="+value));
    }
    
    /**
     * Delete data from database.
     * 
     * @param table
     * @param where
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static void delete(Table table, Where where) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            open();
        String query = "DELETE FROM "+Database.getDbName()+"."+table.getTable()+" "+where.getWhere();
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        executeUpdate(query);
        if (getAutomatic())
            close();
    }
    
    /**
     * Drop the specified databases.
     * 
     * @param databases
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static void dropDatabase(String[] databases) throws SQLException, ClassNotFoundException {
        for (int i=0; i<databases.length; i++)
            dropDatabase(databases[i]);
    }

    /**
     * Drop the specified database.
     *
     * @param database
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static void dropDatabase(String database) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            open();
        String query = "DROP DATABASE "+database;
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        executeUpdate(query);
        if (getAutomatic())
            close();
    }
    
    /**
     * Drop the specified tables.
     * 
     * @param tables
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static void dropTable(Table[] tables) throws SQLException, ClassNotFoundException {
        for (int i=0; i<tables.length; i++)
            dropTable(tables[i]);
    }

    /**
     * Drop the specified table.
     *
     * @param table
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static void dropTable(Table table) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            open();
        String query = "DROP TABLE "+Database.getDbName()+"."+table.getTable();
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        executeUpdate(query);
        if (getAutomatic())
            close();
    }

    /**
     * Remove all record(s) in your tables.
     * 
     * @param tables
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static void truncate(Table[] tables) throws SQLException, ClassNotFoundException {
        for (int i=0; i<tables.length; i++)
            truncate(tables[i]);
    }
    
    /**
     * Remove all record(s) in your table.
     *
     * @param table
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static void truncate(Table table) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            open();
        String query = "TRUNCATE "+Database.getDbName()+"."+table.getTable();
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(query);
        executeUpdate(query);
        if (getAutomatic())
            close();
    }

    /**
     * Execute manual query.
     *
     * @param sql
     * @return ResultSet
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static ResultSet executeQuery(String sql) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            reset();
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(sql);
        return Database.getStat().executeQuery(sql);
    }

    /**
     * Execute manual query.
     *
     * @param sql
     * @return executed query
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static int executeUpdate(String sql) throws SQLException, ClassNotFoundException {
        if (getAutomatic())
            reset();
        if (Database.getShowSQL() == Database.SHOW_SQL)
            System.out.println(sql);
        return Database.getStat().executeUpdate(sql);
    }
    
    /**
     * Export your current database.
     * 
     * @param backupPath 
     */
    public static void exportDatabase(String backupPath) {
        if (Database.getProduct() == Database.MYSQL) {
            exportDatabase("mysqldump", backupPath);
        }
    }
    
    /**
     * Export your current database.
     * 
     * @param mysqlDumpPath
     * @param backupPath 
     */
    public static void exportDatabase(String dumpPath, String backupPath) {
        String pass = "";
        try {
            if (getProduct() == Database.MYSQL) {
                if (getPass() != null && !getPass().equals("")) {
                    pass = "-p "+getPass();
                }
                String sql = "\""+dumpPath+"\" --host "+getIp()+" --port "+getPort()+
                             " -u "+getUser()+" "+pass+" "+getDbName()+" --compact --skip-comments > \""+backupPath+"\"";
                if (getShowSQL() == SHOW_SQL) {
                    System.out.println(sql);
                }
                Util.runApplication(sql);
                
            }
        } catch (IOException ex) {
            ExceptionManager.addException(ex);
        }
    }
    
    /**
     * Import your current database.
     * 
     * @param backupPath 
     */
    public static void importDatabase(String backupPath) {
        if (Database.getProduct() == Database.MYSQL) {
            importDatabase("mysqldump", backupPath);
        }
    }
    
    /**
     * Import your current database.
     * 
     * @param dumpPath
     * @param backupPath 
     */
    public static void importDatabase(String dumpPath, String backupPath) {
        String pass = "";
        try {
            if (getProduct() == MYSQL) {
                if (getPass() != null && !getPass().equals("")) {
                    pass = "-p "+getPass();
                }
                String sql = "\""+dumpPath+"\" --host "+getIp()+" --port "+getPort()+
                             " -u "+getUser()+" "+pass+" --compact --skip-comments > \""+backupPath+"\"";
                if (getShowSQL() == SHOW_SQL) {
                    System.out.println(sql);
                }
                Util.runApplication(sql);
            }
        } catch (IOException ex) {
            ExceptionManager.addException(ex);
        }
    }

}